/**
 * custom radio widget.
 **/
(function($){
	var get_prefix = function(id){
		$.getJSON("/get_prefix/", {'id': id}, function(data){
			var html = "<option value=''>---------</option>";
			$.each(data.pre, function(k, v){
				if($("#id_platform").val() == "A"){
					v1 = v.replace(/-/g, "_");
				}else{
					v1 = v;
				}
				if($("#id_bundle_id_prefix").val() == v1){
					html += "<option selected='selected' value='" + v1 + "'>" + v1 + "</option>";
				}else{
					html += "<option value='" + v1 + "'>" + v1 + "</option>";
				}
			})
			$("#bundle_id_prefix_select").html(html);
		});
	};

	var get_studios = function(store){
		$.getJSON("/get_studios/", {'store': store}, function(data){
			var html = "<option value=''>---------</option>";
			$.each(data.studios, function(k, v){
				html += "<option value='" + v.id + "'>" + v.name + "</option>";
			})
			$("#id_studio").html(html);
		})
	};

	$(document).ready(function(){
		var get_format = function(store){
			$.getJSON("/get_format/", {'id': $("#id_studio option:selected").val(), 'store': store}, function(data){
				$("#id_prj_code").val(data.res);
			})
		}
		
		var do_save = function(){
			var r = confirm("Double check your app information before click 'Yes'! Are you sure to submit now?");
			if(r == true){
				return true;
			}else{
				return false;
			}
		};
		$(".btn.btn-high.btn-info[name=_save]").click(do_save);
		$(".btn.btn-high[name=_continue]").click(do_save);
		$(".btn[name=_addanother]").click(do_save);
		
		$("#id_media_local").bind('input', function(){
			if($(this).val() != ""){
				var local = $(this).val().replace(/\\/g, '/');
				if(local.indexOf(":") > -1){
					var prefix = local.split(":", 1);
					$("#id_media_remote").val(local.replace(prefix + ":", 'https://www.dropbox.com/home/Small Tree Media/Project Submission Doc'));
				}else{
					var prefix = local.split("/Project Submission Doc/", 1);
					$("#id_media_remote").val(local.replace(prefix, 'https://www.dropbox.com/home/Small Tree Media'));
				}
			}else{
				$("#id_media_remote").val("");
			}
		});
 		
		$("#id_app_store").css("display", "none");
		$("#id_app_store").after("\
	<ul id='app_store_radio'> \
	    <li><label><input id='id_app_store_0' name='app_store' type='radio' value='iTunes App Store'> iTunes App Store</label></li> \
	    <li><label><input id='id_app_store_1' name='app_store' type='radio' value='Google Play'> Google Play</label></li> \
	    <li><label><input id='id_app_store_2' name='app_store' type='radio' value='Amazon App Store'> Amazon App Store</label></li> \
	    <li><label><input id='id_app_store_3' name='app_store' type='radio' value=''> Other <input type='text' id='app_store_other' /></label></li> \
	</ul>");
		$("input[type='radio'][name='app_store'][value='" + $("#id_app_store").val() + "']").attr("checked", "checked");
		var current = $("#id_app_store").val();
		if(current != 'iTunes App Store' && current != 'Google Play' && current != 'Amazon App Store'){
			$("#id_app_store_3").val(current);
			$("#id_app_store_3").attr("checked", "checked");
			$("#app_store_other").val(current);
		}
		$("#app_store_radio").change(function(e){
			get_format(e.target.value)
			
			if(e.target.id == 'id_app_store_0'){
				$("div.control-group.form-row.field-store_link ").css('display', 'none');
				$("#id_platform option[value='I']").attr("selected", "selected");
				$("#id_bundle_id_suffix").val("");
				get_prefix($("#id_studio option:selected").val());
			}
			if(e.target.id == 'id_app_store_1' || e.target.id == 'id_app_store_2'){
				$("div.control-group.form-row.field-store_link ").css('display', 'none');
				$("#id_platform option[value='A']").attr("selected", "selected");
				$("#id_bundle_id_suffix").val("android_");
				get_prefix($("#id_studio option:selected").val());
			}
			if(e.target.id != 'id_app_store_3'){
				$("#id_app_store").val(e.target.value);
				get_studios(e.target.value);
			}else{
				get_studios($("#app_store_other").val());
			}
		});
		$("#app_store_other").change(function(){
			$("#id_app_store_3").attr('checked', 'checked');
			$("#id_app_store_3").val($("#app_store_other").val());
			$("#id_app_store").val($("#app_store_other").val());
		});

		$("#id_platform").change(function(e){
			if(e.target.value == "A"){
				$("#id_bundle_id_suffix").val("android_");
			}else{
				$("#id_bundle_id_suffix").val("");
			};
			get_prefix($("#id_studio option:selected").val());
		});
		
		
		$("td.field-fe > select").css('width', '140px');
		
		$("div.field-notes").css('display', 'none');

		if($("#id_status").val() == 'I'){
			$("div.field-notes").css('display', 'block');
		};
		
		if($("div.field-status > div > div.controls > span").text() == 'Invalid'){
			$("div.field-notes").css('display', 'block');
		};

		$("#id_status").change(function(e){
			if(e.target.value == 'I'){
				$("div.field-notes").css('display', 'block');
			}else{
				$("div.field-notes").css('display', 'none');
			}
		});
		
		$.each($("span.readonly"), function(){
			$(this).parent().children("span.help-inline").css('display', 'none');
		});
		
	});
})(django.jQuery);
